package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import com.atguigu.gmall.product.mapper.SpuSaleAttrMapper;
import com.atguigu.gmall.product.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jodd.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SpuManageServiceImpl implements SpuManageService {
    @Autowired
    private SpuInfoMapper spuInfoMapper;
    @Autowired
    private SpuImageServie spuImageServie;
    @Autowired
    private SpuPosterService spuPosterService;
    @Autowired
    private SpuSaleAttrService spuSaleAttrService;
    @Autowired
    private SpuSaleAttrValueService spuSaleAttrValueService;
    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;

    /**
     * 获取SPU分页列表
     * @param category3Id
     * @return
     */
    @Override
    public IPage<SpuInfo> getSpuByPage(Long category3Id, Page<SpuInfo> pageInfo) {
        LambdaQueryWrapper<SpuInfo> spuInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        spuInfoLambdaQueryWrapper.eq(SpuInfo::getCategory3Id, category3Id);
        spuInfoLambdaQueryWrapper.orderByDesc(SpuInfo::getId);
        Page<SpuInfo> spuInfoPage = spuInfoMapper.selectPage(pageInfo, spuInfoLambdaQueryWrapper);
        return spuInfoPage;
    }

    /**
     * 保存或修改spuInfo
     * @param spuInfo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSpuInfo(SpuInfo spuInfo) {
        if (spuInfo.getId() != null) {
            spuInfoMapper.updateById(spuInfo);
            spuImageServie.remove(new LambdaQueryWrapper<SpuImage>().eq(SpuImage::getSpuId, spuInfo.getId()));
            spuPosterService.remove(new LambdaQueryWrapper<SpuPoster>().eq(SpuPoster::getSpuId, spuInfo.getId()));
            spuSaleAttrService.remove(new LambdaQueryWrapper<SpuSaleAttr>().eq(SpuSaleAttr::getSpuId, spuInfo.getId()));
            spuSaleAttrValueService.remove(new LambdaQueryWrapper<SpuSaleAttrValue>().eq(SpuSaleAttrValue::getSpuId, spuInfo.getId()));
        } else {
            //保存spu信息
            spuInfoMapper.insert(spuInfo);
        }
        //保存商品图片集合
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        if (!CollectionUtils.isEmpty(spuImageList)) {
            spuImageList.forEach(spuImage -> spuImage.setSpuId(spuInfo.getId()));
            spuImageServie.saveBatch(spuImageList);
        }
        //保存商品海报图片集合
        List<SpuPoster> spuPosterList = spuInfo.getSpuPosterList();
        if (!CollectionUtils.isEmpty(spuImageList)) {
            spuPosterList.forEach(spuPoster -> spuPoster.setSpuId(spuInfo.getId()));
            spuPosterService.saveBatch(spuPosterList);
        }
        //保存销售属性
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        if (!CollectionUtils.isEmpty(spuSaleAttrList)) {
            //保存销售属性值
            spuSaleAttrList.forEach(spuSaleAttr -> {
                spuSaleAttr.setSpuId(spuInfo.getId());
                List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
                if (!CollectionUtils.isEmpty(spuSaleAttrValueList)) {
                    spuSaleAttrValueList.forEach(spuSaleAttrValue -> {
                        spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                        spuSaleAttrValue.setSpuId(spuInfo.getId());
                    });
                    spuSaleAttrValueService.saveBatch(spuSaleAttrValueList);
                }
            });
            spuSaleAttrService.saveBatch(spuSaleAttrList);
        }
    }

    /**
     *获取spuInfo集合
     * @param spuId
     * @return
     */
    @Override
    public List<SpuSaleAttr> getSpuSaleAttrList(Long spuId) {
        List<SpuSaleAttr> spuSaleAttrList = spuSaleAttrMapper.getSpuSaleAttrList(spuId);
        return spuSaleAttrList;
    }

    /**
     *根据spuId获取spuInfo
     * @param spuId
     * @return
     */
    @Override
    public SpuInfo getSpuInfo(Long spuId) {
        //查询spu基本信息
        SpuInfo spuInfo = spuInfoMapper.selectById(spuId);
        if (spuInfo != null) {
            //查询spu图片
            List<SpuImage> spuImageList = spuImageServie.list(new LambdaQueryWrapper<SpuImage>().eq(SpuImage::getSpuId, spuId));
            if (!CollectionUtils.isEmpty(spuImageList)) {
                spuInfo.setSpuImageList(spuImageList);
            }
            //查询spu海报
            List<SpuPoster> spuPosterList = spuPosterService.list(new LambdaQueryWrapper<SpuPoster>().eq(SpuPoster::getSpuId, spuId));
            if (!CollectionUtils.isEmpty(spuPosterList)) {
                spuInfo.setSpuPosterList(spuPosterList);
            }
            //查询spu销售属性和销售属性值
            List<SpuSaleAttr> spuSaleAttrList = spuSaleAttrMapper.getSpuSaleAttrList(spuId);
            if (!CollectionUtils.isEmpty(spuSaleAttrList)) {
                spuInfo.setSpuSaleAttrList(spuSaleAttrList);
            }
        }

        return spuInfo;
    }

}
